import * as api from "../api/api";
import request from "../utils/request";
import { pageSize } from "../utils/common";
export default {
  namespace: "mall",
  state: {
    storeId: 1,
    banner: {},
    banners: { items: [] },
    stores: { items: [] },
    pointRule: {},
    pointRules: { items: [] },
    releasePointRule: {},
    releasePointRules: { items: [] },
    userPoints: { totalCount: 0, items: [] },
    userPointSearchs: {
      pageIndex: 1,
      pageSize,
      keywords: "",
      pointType: "",
      pointStatus: ""
    },
    active: {},
    actives: { items: [] }
  },
  effects: {
    /**
     * 获取用户体验金明细
     * @param param0
     * @param param1
     */
    *getAllUserPoints({ payload }, { call, put }) {
      const { keywords, pointType, pointStatus, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formUserPointsStatus = pointStatus,
        formUserPointsType = pointType,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userPoints = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetAllUserPointsAsync(
          {
            formUserPointsStatus,
            formUserPointsType,
            formKeywords,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userPoints,
          userPointSearchs: {
            keywords,
            pointStatus,
            pointType,
            pageIndex,
            pageSize
          }
        }
      });
    },
    /**
     * 获取店铺列表
     * @param param0
     * @param param1
     */
    *getStores({ payload }, { call, put }) {
      const { callback } = payload;
      const stores: api.AbpApplicationServicesDtoListResultDtoSingleUserMallStoresVOStoreVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoresAsync(
          {}
        )
      );
      const storeId = stores.items && stores.items[0].id;
      yield put({ type: "save", payload: { stores } });
      yield put({ type: "save", payload: { storeId } });
      callback && callback(true);
    },
    /**
     * 获取banner列表
     * @param param0
     * @param param1
     */
    *getBanners({ payload }, { call, put }) {
      const { storeId } = payload;
      if (storeId == "请选择") return;
      const formStoreId = storeId;
      const banners = yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageGetBannersAsync(
          {
            formStoreId
          }
        )
      );
      yield put({ type: "save", payload: { banners } });
    },
    /**
     * 获取活动列表
     * @param param0
     * @param param1
     */
    *getActives({ payload }, { call, put }) {
      const actives = yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageGetActivesAsync(
          {}
        )
      );
      yield put({ type: "save", payload: { actives } });
    },
    /**
     * 获取体验金规则列表
     * @param param0
     * @param param1
     */
    *getPointRules({ payload }, { call, put }) {
      const pointRules = yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageGetPointRulesAsync(
          {}
        )
      );
      yield put({ type: "save", payload: { pointRules } });
    },
    /**
     * 获取释放体验金规则列表
     * @param param0
     * @param param1
     */
    *getReleasePointRules({ payload }, { call, put }) {
      const releasePointRules = yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageGetReleasePointRulesAsync(
          {}
        )
      );
      yield put({ type: "save", payload: { releasePointRules } });
    },
    /**
     * 获取banner详情
     * @param param0
     * @param param1
     */
    *getBanner({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const banner = yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageGetBannerAsync(
          {
            formId
          }
        )
      );
      yield put({ type: "save", payload: { banner } });
    },
    /**
     * 保存banner
     * @param param0
     * @param param1
     */
    *saveBanner({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageSaveBannerAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 保存活动
     * @param param0
     * @param param1
     */
    *saveActive({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageSaveActiveAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 保存体验金规则
     * @param param0
     * @param param1
     */
    *savePointRule({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageSavePointRulesAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 保存释放体验金规则
     * @param param0
     * @param param1
     */
    *saveReleasePointRule({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageSaveReleasePointRulesAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 删除释放体验金规则
     * @param param0
     * @param param1
     */
    *removeReleasePointRule({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageRemoveReleasePointAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 删除banner
     * @param param0
     * @param param1
     */
    *removeBanner({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageRemoveBannerAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 上移banner
     * @param param0
     * @param param1
     */
    *moveUpBanner({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageMoveUpBannerAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 下移banner
     * @param param0
     * @param param1
     */
    *moveDownBanner({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageMoveDownBannerAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 上移活动
     * @param param0
     * @param param1
     */
    *moveUpActive({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageMoveUpActiveAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 下移活动
     * @param param0
     * @param param1
     */
    *moveDownActive({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagemallManageApiFetchParamCreactor.managemallManageMoveDownActiveAsync(
          { form }
        )
      );
      callback && callback(true);
    }
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {}
};
